package com.springboot.web.aop;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

@Aspect//切面
@Component//注册到spring中
public class Audience {

    @Pointcut("execution(* com.springboot.web.aop.Performer.perform(..))")
    public void method () {}

    @Before("method()")
    public void takeSeats() {
        System.out.println("观众找座位坐下");
    }

    @Before("method()")
    public void turnOffCellPhones() {
        System.out.println("观众关闭手机");
    }

    @AfterReturning("method()")
    public void applause() {
        System.out.println("观众鼓掌");
    }

    @AfterThrowing("method()")
    public void throwEggs() {
        System.out.println("观众抛鸡蛋");
    }

    @Around("method()")
    public String around (ProceedingJoinPoint proceedingJoinPoint) {
        try {
            System.out.println("环绕通知的前置部分");
            proceedingJoinPoint.proceed();
            System.out.println("环绕通知的后置部分");
        } catch (Throwable e) {
            System.out.println("环绕通知的异常部分");
            throw new RuntimeException(e);
        }
        return "success";
    }
}
